package com.gx.smart.base;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.BarUtils;
import com.gx.smart.base.databinding.ActivityRoot2Binding;
import com.gx.smart.base.loading.LoadingView;

import org.greenrobot.eventbus.EventBus;

/**
 * 基础activity
 * 带EditText的界面
 * 统一处理带EditText界面的title问题
 */

public abstract class BaseMVVMActivityV3<VM extends BaseViewModel> extends AppCompatActivity {

    protected VM viewModel;
    protected ActivityRoot2Binding binding;

    protected LoadingView mLoadingInitView;
    protected View mNoDataView;
    protected TextView mTxtTitle;
    protected Toolbar mToolbar;
    protected RelativeLayout mViewStubContent;
    private ViewStub mViewStubToolbar;
    private ViewStub mViewStubInitLoading;
    private ViewStub mViewStubNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusBar();
        binding = ActivityRoot2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initViewModel();
        initView();
        initContent();
        initData();
        initObserver();
    }


    private void initViewModel() {
        viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(onBindViewModel());
    }


    protected void initData() {

    }

    protected void initObserver() {

    }

    protected abstract Class<VM> onBindViewModel();

    /**
     * 绑定布局
     *
     * @return
     */
    @LayoutRes
    protected abstract int onBindLayout();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    protected void unregisterEventBus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().removeAllStickyEvents();
            EventBus.getDefault().unregister(this);
        }
    }


    protected void initStatusBar() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setStatusBarLightMode(this, true);
    }


    protected void initView() {

    }

    protected void initContent() {
        initCommonView();
    }

    public void initCommonView() {
        ViewGroup.LayoutParams params = binding.statusBar.getLayoutParams();
        params.height = BarUtils.getStatusBarHeight();
        binding.statusBar.setLayoutParams(params);

        mViewStubToolbar = findViewById(R.id.view_stub_toolbar);
        mViewStubContent = findViewById(R.id.view_stub_content);
        mViewStubInitLoading = findViewById(R.id.view_stub_init_loading);
        mViewStubNoData = findViewById(R.id.view_stub_nodata);

        if (enableToolbar()) {
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            View viewTooBar = mViewStubToolbar.inflate();
            initTooBar(viewTooBar);
        }
        initContentView(mViewStubContent);
    }

    public void initContentView(ViewGroup root) {
        LayoutInflater.from(this).inflate(onBindLayout(), root, true);
    }

    public int onBindToolbarLayout() {
        return R.layout.common_toolbar;
    }

    public boolean enableToolbar() {
        return true;
    }

    protected void initTooBar(View view) {
        mToolbar = view.findViewById(R.id.toolbar);
        mTxtTitle = view.findViewById(R.id.title);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
        if (mTxtTitle != null) {
            mTxtTitle.setText(getToolbarTitle());
        }
    }

    public abstract String getToolbarTitle();


    public void showInitLoadView(boolean show) {
        if (mLoadingInitView == null) {
            View view = mViewStubInitLoading.inflate();
            mLoadingInitView = view.findViewById(R.id.loadingView);
        }
        mLoadingInitView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void showNoDataView(boolean show) {
        if (mNoDataView == null) {
            View view = mViewStubNoData.inflate();
            mNoDataView = view.findViewById(R.id.view_no_data);
        }
        mNoDataView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


}